﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using BusinessLogic;

namespace Manager
{
    public partial class ServicePrice : System.Web.UI.Page
    {
        string text = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            ucPaging.PageChange += new global::Manager.Controls.ucPaging.PagingHandler(Load_Data);
            if (!Page.IsPostBack)
            {
                Load_Data();
                ucUpdate.IsNew = true;
            }
        }

        #region Load Data
        private void Load_Data()
        {
            int totalRecord = 0;
            ucPaging.PageSize = 20;
            ucPaging.PageDisplay = 5;
            ServicePriceBusiness servicePriceBusiness= new ServicePriceBusiness();
            DataTable dtbServicePrice = servicePriceBusiness.Search(text, ucPaging.CurrentPage, ucPaging.PageSize, ref totalRecord);
            ucPaging.TotalRecord = totalRecord;
            ucPaging.Visible = ucPaging.TotalPage > 1;

            if ((dtbServicePrice != null) && (dtbServicePrice.Rows.Count > 0))
            {
                rptServicePrice.DataSource = dtbServicePrice;
                rptServicePrice.DataBind();
            }
        }
        #endregion
        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            text = txtSearch.Text.Trim();
            Load_Data();
        }

        protected void rptServicePrice_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lblDesc = (Label)e.Item.FindControl("lblDesc");
            Label lblStatus = (Label)e.Item.FindControl("lblStatus");
            int servicePriceID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ServicePriceID"));
            ServicePriceInfo servicePriceInfo = ServicePriceData.SelectByServicePriceID(servicePriceID);

            string desc = servicePriceInfo.ServiceDescription;
            if (desc.Length >= 200)
            {
                desc = desc.Substring(0, 200);
            }

            lblDesc.Text = desc;
            if (servicePriceInfo.ServiceStatus == true)
            {
                lblStatus.Text = "Hiển thị";
            } 
            else
            {
                lblStatus.Text = "Không Hiển thị";
            }
        }

        protected void rptServicePrice_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int servicePriceID=Convert.ToInt32(e.CommandArgument);

            if (e.CommandName == "View")
            {
                Session["sesID"] = servicePriceID.ToString();
                ucUpdate.Load_ServicePrice(servicePriceID);
                ucUpdate.IsNew = false;
            }

            if (e.CommandName == "Del")
            {
                ServicePriceData.DeleteByServicePriceID(servicePriceID);
                Response.Redirect("ServicePrice.aspx");
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            ucUpdate.IsNew = true;
            ucUpdate.Reset_Control();
        }
    }
}